package com.yafeed.trade.common.utils;

import java.text.SimpleDateFormat;

/**
 * @description: ID 序列
 * @author: yanghj
 * @time: 2020-12-13 20:23
 */
public class IdGenerator {
    /**
     * 20位末尾的数字id
     */
    private static volatile int Guid = 100;

    public static Long getGuid() {

        IdGenerator.Guid += 1;

        long now = System.currentTimeMillis();
        //获取4位年份数字
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");
        //获取时间戳
        String time = dateFormat.format(now);
        String info = now + "";
        //获取三位随机数
        //int ran=(int) ((Math.random()*9+1)*100);
        //要是一段时间内的数据连过大会有重复的情况，所以做以下修改
        int ran = 0;
        if (IdGenerator.Guid > 999) {
            IdGenerator.Guid = 100;
        }
        ran = IdGenerator.Guid;

        String id = time + info.substring(2, info.length()) + ran;
        return Long.parseLong(id);
    }
}